package yicr.untils.gis.wireless;


import yicr.untils.NslUtilCollection;
import yicr.untils.gis.Polygon.NslPolygon;
import yicr.untils.gis.Polygon.NslUtilPolygon;
import yicr.untils.gis.wireless.WirelessCell;
import yicr.untils.gis.geoHash.GeoHash;
import yicr.untils.gis.geoHash.NslUtilGeoHash;

import java.util.*;

public class NslUtilWireless {

    /**
     * 根据小区经纬度、方向角、扇形角度、半径，生成 扇形polygon
     * @param cell
     * @param radius
     * @return
     */
    public static NslPolygon buildPolygonByCell(WirelessCell cell, Integer radius, Integer sectorAngle){

        String id= cell.getId()+"";
        //将小区的方向角转换成 坐标系的角度
        double axisAngle=90.0-cell.getDirectionAngle();
        double startAngle=axisAngle-sectorAngle/2.0;
        double endAngle=axisAngle+sectorAngle/2.0;

        NslPolygon polygon= NslUtilPolygon.createSector(cell.getLongitude(),cell.getLatitude(),radius,startAngle,endAngle);
        polygon.setId(cell.getId());
        return polygon;
    }

    public static List<NslPolygon> buildPolygonListByCellList(List<WirelessCell> cellList,Integer radius,Integer sectorAngle){
        NslUtilCollection.assetNotNullEmpty(cellList);
        List<NslPolygon> polygonList=new ArrayList<>();
        for (WirelessCell wirelessCell : cellList) {
            NslPolygon polygon=buildPolygonByCell(wirelessCell,radius,sectorAngle);
            polygonList.add(polygon);
        }
        return polygonList;
    }

    public static List<NslPolygon> buildPolygonListByCellList(Collection<WirelessCell> cellCollection){
        NslUtilCollection.assetNotNullEmpty(cellCollection);
        List<NslPolygon> polygonList=new ArrayList<>();
        for (WirelessCell wirelessCell : cellCollection) {
            NslPolygon polygon=buildPolygonByCell(wirelessCell, (int) wirelessCell.getCoverRadius(), (int) wirelessCell.getHorizontalBeam());
            polygonList.add(polygon);
        }
        return polygonList;
    }

    /**
     *  根据小区 覆盖半径、方位角和水平波瓣宽度 生成 覆盖扇形
     * @param wirelessCell
     * @return
     */
    public static NslPolygon buildPolygonListByCell(WirelessCell wirelessCell){
        double lon=wirelessCell.getLongitude();
        double lat=wirelessCell.getLatitude();
        double radius=wirelessCell.getCoverRadius();
        //将小区的方向角转换成 坐标系的角度
        double sectorAngle=wirelessCell.getHorizontalBeam();
        double axisAngle=90.0-wirelessCell.getDirectionAngle();
        double startAngle=axisAngle-sectorAngle/2.0;
        double endAngle=axisAngle+sectorAngle/2.0;
        NslPolygon polygon=NslUtilPolygon.createSector(lon,lat,radius,startAngle,endAngle);
        polygon.setId(wirelessCell.getId());
        return polygon;
    }



    /**
     * 根据小区的覆盖半径、水平波瓣宽度、方向角生成的扇形覆盖范围，对在此扇形覆盖内的geoHash进行分组
     * 即 计算 每个小区覆盖范围 包含的 geoHash 集
     * @param cellCollection
     * @param geoHashList
     * @return
     */
    public static Map<WirelessCell,Set<GeoHash>> calculateGeoHashSetOfCell(Collection<WirelessCell> cellCollection, Collection<GeoHash> geoHashList){
        Map<WirelessCell,Set<GeoHash>> matchMap=new HashMap<>();
        Set<WirelessCell> cellSet=new HashSet<>();
        cellSet.addAll(cellCollection);


        Map<WirelessCell,NslPolygon> cellNslPolygonMap=new HashMap<>();

        for (WirelessCell cell : cellSet) {
            NslPolygon polygon=buildPolygonListByCell(cell);
            cellNslPolygonMap.put(cell,polygon);
        }

        for (WirelessCell cell : cellSet) {
            NslPolygon polygon=cellNslPolygonMap.get(cell);
            Set<GeoHash> geoHashSet= NslUtilGeoHash.calculateGeoHashListOfPolygon(polygon,geoHashList);
            matchMap.put(cell,geoHashSet);
        }
        return matchMap;
    }
}
